--=================================--
socket = require ("socket")
print(socket._VERSION)

if not socket then
    print("load socket module failed.")
else
    print("success load socket module.")
    local host = "192.168.1.229"
    local port = "6000"
    lua_server = assert(socket.bind(host, port,1024))
    lua_server:settimeout(0)
    print("Server Start " .. host .. ":" .. port)
end

--while 1 do
    local client = lua_server:accept()
    if client then
        print("A client successfully connect!")
    end

    client:send("AT+STACH1=1\r\n")
    local receive,receive_status = client:receive()
    if receive_status ~= "close" then
        assert(client:send("Client  Send : "..receive .. "\n"))
        print("Receive Client : " .. receive)
    end

    client:send("AT+STACH1=0\r\n")
    local receive,receive_status = client:receive()
    if receive_status ~= "close" then
        assert(client:send("Client  Send : "..receive .. "\n"))
        print("Receive Client : " .. receive)
    end


    client:send("AT+OCCH1=0\r\n")
    local receive,receive_status = client:receive()
    if receive_status ~= "close" then
        assert(client:send("Client  Send : "..receive .. "\n"))
        print("Receive Client : " .. receive)
    end

    client:close()
    lua_server:close()

    --local recvt, sendt, status = socket.select({client}, nil, 1)
    --if #recvt > 0 then
    --    local receive, receive_status = client:receive()
    --    if receive_status ~= "closed" then
    --        if receive then
    --            assert(client:send("Client  Send : "..receive .. "\n"))
    --            print("Receive Client : " .. receive)
    --        end
    --    else
    --        client:close()
    --        print("Client disconnect!")
    --    end
    --end
--end
